//页面加载完毕后执行获取用户信息
$(function(){
	// 数量减
	$(".minus").click(function() {
		var t = $(this).parent().find('.num');
		t.text(parseInt(t.text()) - 1);
		if (t.text() <= 1) {
			t.text(1);
		}
		totalPrice(t.text());
	});
	// 数量加
	$(".plus").click(function() {
		var t = $(this).parent().find('.num');
		t.text(parseInt(t.text()) + 1);
		if (t.text() <= 1) {
			t.text(1);
		}
		totalPrice(t.text());
	});
	function totalPrice(num){
		var price = $("#price").val();
		var totalAmount = (price*parseInt(num)).toFixed(2);
		$("#span-total-amount").html("￥"+totalAmount);
		$("#span-em-total-amount").html("￥"+totalAmount);
	}
	
	var loginNameCookie = $.cookie("gou-loginName");
	if(loginNameCookie == null || loginNameCookie == ''){
		loginNameCookie = "0";
	}else{
		$("#strong-mobile").html("手机号："+loginNameCookie);
		$("#buyerCode").val(loginNameCookie);
	}
	var ahref = $("#a-user-info-href").attr("href");
	$("#a-user-info-href").attr("href",ahref+'/'+loginNameCookie);
	
	var nameCookie = $.cookie("gou-name");
	if(nameCookie != null && loginNameCookie != ''){
		$("#strong-name").html(nameCookie);
		$("#buyerName").val(nameCookie);
	}
	var weixinCookie = $.cookie("gou-weixin");
	if(weixinCookie != null && weixinCookie != ''){
		$("#strong-weixin").html("微信号："+weixinCookie);
		$("#buyerWeixin").val(weixinCookie);
	}
	var addressCookie = $.cookie("gou-address");
	if(addressCookie != null && addressCookie != ''){
		$("#li-address").html("<i class='aui-icon aui-icon-address'></i>"+addressCookie);
		$("#address").val(addressCookie);
	}
	var emailCookie = $.cookie("gou-email");
	if(emailCookie != null && emailCookie != ''){
		$("#strong-email").html("邮箱："+emailCookie);
	}
})


//提交订单
$(function(){
	function submitOrder(){
		//判断冻结标记"frezz"
		var buttonFlag = $('#buttonFlag').val();
		var nowFlag = new Date().valueOf();
		if(buttonFlag != null && buttonFlag != ""){
			if(nowFlag - buttonFlag < 2000){
				return;
			}
		}
		//设置冻结
		$('#buttonFlag').val(nowFlag);
		//获取form的值
		var id = $('#id').val();
		var shopId = $('#shopId').val();
		var ts = $('#ts').val();
		var productId = $('#productId').val();
		var productName = $('#productName').val();
		var price = $('#price').val();
		var num = $('#productNum').html();
		
		var spec = $('#spec').val();
		var cate = $('#cate').val();
		var brand = $('#brand').val();
		var productionPlace = $('#productionPlace').val();
		
		
		var buyerCode = $('#buyerCode').val();
		var buyerName = $('#buyerName').val();
		var buyerWeixin = $('#buyerWeixin').val();
		var address = $('#address').val();
		
		var status = $('#status').val();
		var payStatus = $('#payStatus').val();
		var remark = $('#remark').val();
		var picMainImage = $('#picMainImage').attr("src");
		
		var sendCompany = $('#sendCompany').val();
		var sendNo = $('#sendNo').val();
		var sendTime = $('#sendTime').val();
		//是不是草稿，下单也必须有商品
		if(productName == null || productName == ""){
			layer.msg("请选择一个商品或自定义商品名称!",{icon:2,time:2000});
			return;
		}
		//如果不是草稿，需以下校验
		if(price == null || price == ""){
			layer.msg("请输入商品价格!",{icon:2,time:2000});
			return;
		}
		if(num == null || num == ""){
			layer.msg("请输入购买数量!",{icon:2,time:2000});
			return;
		}
		
		if(buyerCode == null || buyerCode == ""){
			layer.msg("请输入购买人联系电话!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(buyerCode)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		if(buyerName == null || buyerName == ""){
			layer.msg("请输入购买人姓名!",{icon:2,time:2000});
			return;
		}
		
		if(address == null || address == ""){
			layer.msg("请输入购买人收货地址!",{icon:2,time:2000});
			return;
		}
		
		if(picMainImage == null || picMainImage == ""){
			layer.msg("请上传商品图片,方便向客户展示!",{icon:2,time:2000});
			return;
		}
		if(buyerWeixin != null && buyerWeixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(productName != null && productName.length > 32){
			layer.msg("商品名称不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(picMainImage != null && picMainImage.length > 512){
			layer.msg("商品图片url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(remark != null && remark.length > 512){
			layer.msg("备注不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(spec != null && spec.length > 32){
			layer.msg("规格不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(cate != null && cate.length > 32){
			layer.msg("类别不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(brand != null && brand.length > 32){
			layer.msg("品牌不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(productionPlace != null && productionPlace.length > 100){
			layer.msg("产地不能超过100位!",{icon:2,time:2000});
			return;
		}
		if(sendCompany != null && sendCompany.length > 32){
			layer.msg("快递公司不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(sendNo != null && sendNo.length > 32){
			layer.msg("快递单号不能超过32位!",{icon:2,time:2000});
			return;
		}
		
		var order = {};
		order["id"] = id;
		order["shopId"] = shopId;
		order["ts"] = ts;
		order["productId"] = productId;
		order["productName"] = productName;
		order["price"] = price;
		order["num"] = num;
		
		order["status"] = status;
		order["payStatus"] = payStatus;
		
		order["picMain"] = picMainImage;
		order["remark"] = remark;
		
		order["buyerCode"] = buyerCode;
		order["buyerName"] = buyerName;
		order["buyerWeixin"] = buyerWeixin;
		order["address"] = address;
		
		order["spec"] = spec;
		order["cate"] = cate;
		order["brand"] = brand;
		order["productionPlace"] = productionPlace;
		
		order["sendCompany"] = sendCompany;
		order["sendNo"] = sendNo;
		order["sendTime"] = sendTime;
		
		$.ajax({
			url:baselocation+'/order',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(order),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/order/'+result.data+'/pay';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}
	
	$('#submitOrder').click(function () {
		submitOrder();
	})
});
function updateOrderStatus(id,ts,status){
	var messageAlert;
	if(status == '7'){
		messageAlert = "取消订单？";
	}else if(status == '4'){
		messageAlert = "收货？";
	}else if(status == '5'){
		messageAlert = "订单完成？";
	}
	layer.confirm('是否确认'+messageAlert, {
	    btn: ['确认','取消'], //按钮
	    icon: 3, 
	    title:'操作提示',
	    skin: 'layui-layer-demo', //样式类名
	    closeBtn: false, //不显示关闭按钮
	    shadeClose: true, //开启遮罩关闭
	    shade: false //不显示遮罩
	}, function(){
		var order = {};
		order["id"] = id;
		order["ts"] = ts;
		order["status"] = status;
		$.ajax({
			url:baselocation+'/order-update-status',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(order),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					layer.msg(result.message,{icon:1,time:2000});
					window.location.reload();
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}, function(){
	    //取消
	});
	
}